<template>
  <el-form
    :model="addEditForm"
    ref="addEditForm"
    label-width="105px"
    label-position="left"
    class="pg-shaohuo-person-add-edit-identity">
    <el-dialog
      :title="`${type}身份类型`"
      :before-close="beforeDialogClose"
      :visible.sync="dialogVisible"
      width="50%">
      <!-- 表单主题
      -------------------------- -->
      <el-form-item 
        label="身份类型名称"
        prop="name"
        :rules="{ required: true, message: '请输入身份类型名称', trigger: 'blur' }">
        <el-input v-model="addEditForm.name"
          placeholder="请输入身份类型名称"></el-input>
      </el-form-item>

      <!-- 选择的类型
      -------------------------- -->
      <el-form-item
        label="选择捎货类型"
        prop="types"
        :rules="{ required: true, message: '选择类型不能为空', trigger: 'blur' }">
        <el-select 
          v-model="addEditForm.types"
          @visible-change="visibleChange"
          :loading="!hasAllTypeList"
          multiple
          collapse-tags
          placeholder="可多选">
          <el-option 
            v-for="item in typeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      
      <!-- 底部按钮区
      -------------------------- -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogClose">取 消</el-button>
        <el-button type="primary"
          @click="submitForm"
          :loading="loadingBtn === '确定'">确 定</el-button>
      </span>
    </el-dialog>
  </el-form>
</template>

<script>
import { ShaohuoPersonService, ShaohuoContentService } from 'services/Api'
import {
  btnLoadingMixin,
  formAddEditMixin,
  dialogCoreMixin
} from 'services/Public'
import { mapState } from 'vuex'

export default {
  name: 'PgTypeAddtype',
  mixins: [
    btnLoadingMixin,
    formAddEditMixin,
    dialogCoreMixin
  ],
  data () {
    return {
      addEditForm: {
        name: '',
        types: []
      },
      typeList: null, // 类型列表
      hasAllTypeList: false // 是否已获取所有类型列表
    }
  },
  computed: {
    ...mapState([ 'acc_id' ]),
    // 可用捎货类型(下拉框使用)参数
    shaohuoTypeParam () { return { acc_id: this.acc_id } },
    // 添加新捎货试题参数
    addShaohuoTypeParam () {
      return {
        acc_id: this.acc_id,
        name: this.addEditForm.name,
        fc_ids: this.addEditForm.types.join(',')
      }
    },
    // 修改捎货试题的内容参数
    alterShaohuoTypeParam () {
      return {
        acc_id: this.acc_id,
        ust_id: this.editData.ust_id,
        name: this.addEditForm.name,
        fc_ids: this.addEditForm.types.join(',')
      }
    }
  },
  methods: {
    // 关闭弹窗前重置表单
    beforeDialogClose (done) {
      if (done) done()
      this.isEditing = false
      this.resetForm()
      this.addEditForm = {
        name: '',
        types: []
      }
    },
    // formAddEditMixin需要 - 编辑时将传入的源数据写入当前表单
    editDataHandler (v) {
      this.addEditForm.name = v.name || ''
      this.addEditForm.types = Object.keys(v.func_names)
      if (!this.hasAllTypeList) {
        const values = Object.values(v.func_names)
        this.typeList = Object.keys(v.func_names)
          .map((key, index) => { return { id: key, name: values[index] } })
      }
    },
    // formAddEditMixin需要 - 提交成功后的操作
    afterSubmitSuccess () {
      this.dialogComplete()
    },
    // 下拉框出现/隐藏时触发
    visibleChange (boo) {
      // 出现时判断widgetList是否已获取，没有则调用接口获取
      if (boo && !this.hasAllTypeList) this.shaohuoTypeApi()
    },
    // 添加新的捎货类型接口
    currentAddApi () {
      return ShaohuoPersonService.addShaohuoType(this.addShaohuoTypeParam)
    },
    // 编辑捎货类型接口
    currentEditApi () {
      return ShaohuoPersonService.alterShaohuoType(this.alterShaohuoTypeParam)
    },
    // 可用捎货类型(下拉框使用)
    async shaohuoTypeApi () {
      const [ err, res ] = await ShaohuoContentService.shaohuoType(this.shaohuoTypeParam)
      if (err) return this.$message.error(err.message || '获取失败')
      this.typeList = res
      this.hasAllTypeList = true
    }
  }
}
</script>

<style lang="scss">
@import "src/styles/config/views.scss";

@include b(shaohuo-person-add-edit-identity) {
  .el-form-item__content > .el-input,
  .el-form-item__content > .el-select {
    width: 80%;
  }
  @include e(delete) {
    padding-left: $--padding-base;
  }
}
</style>
